<html>
<head>
<title> XML/JSON - Tests </title>
<script type="text/javascript" src="xml2json.js"></script>
<script type="text/javascript" src="json2xml.js"></script>
<script type="text/javascript">

var xml=[
      '<e/>',

      '<e>text</e>',

      '<e name="value" />',

      '<e name="value">text</e>',

      '<e> <a>text</a> <b>text</b> </e>',

      '<e> <a>text</a> <a>text</a> </e>',

      '<e> text <a>text</a> </e>',

      '<a>hello</a>',

      '<a x="y">hello</a>',

      '<a id="a"><b id="b">hey!</b></a>',

      '<a>x<c/>y</a>',

      '<x u=""/>',

      '<html>'+
      ' <head>'+
      '   <title>Xml/Json</title>'+
	    '   <meta name="x" content="y" />'+
      ' </head>'+
      ' <body>'+
      ' </body>'+
      '</html>',

      '<ol class="xoxo">'+
      '   <li>Subject 1'+
      '     <ol>'+
      '       <li>subpoint a</li>'+
      '      <li>subpoint b</li>'+
      '     </ol>'+
      '   </li>'+
      '   <li><span>Subject 2</span>'+
      '     <ol compact="compact">'+
      '       <li>subpoint c</li>'+
      '       <li>subpoint d</li>'+
      '     </ol>'+
      '  </li>'+
      '</ol>',

      '<span class="vevent">'+
      '  <a class="url" href="http://www.web2con.com/">'+
      '    <span class="summary">Web 2.0 Conference</span>'+
      '    <abbr class="dtstart" title="2005-10-05">October 5</abbr>'+
      '    <abbr class="dtend" title="2005-10-08">7</abbr>'+
      '    <span class="location">Argent Hotel, San Francisco, CA</span>'+
      '  </a>'+
      '</span>',

      '<e>\n'+
      '  <![CDATA[ .. some data .. ]]>\n'+
      '</e>',

      '<e>\n'+
      '  <a />\n' +
      '  <![CDATA[ .. some data .. ]]>\n'+
      '  <b />\n' +
      '</e>',

      '<e>\n'+
      '  some text\n' +
      '  <![CDATA[ .. some data .. ]]>\n'+
      '  more text\n' +
      '</e>',

      '<e>\n'+
      '  some text\n' +
      '  <![CDATA[ .. some data .. ]]>\n'+
      '  <a />\n' +
      '</e>',

      '<e>\n'+
      '  <![CDATA[ .. some data .. ]]>\n'+
      '  <![CDATA[ .. more data .. ]]>\n'+
      '</e>'
   ];

function parseXml(xml) {
   var dom = null;
   if (window.DOMParser) {
      try { 
         dom = (new DOMParser()).parseFromString(xml, "text/xml"); 
      } 
      catch (e) { dom = null; }
   }
   else if (window.ActiveXObject) {
      try {
         dom = new ActiveXObject('Microsoft.XMLDOM');
         dom.async = false;
         if (!dom.loadXML(xml)) // parse error ..
            window.alert(dom.parseError.reason + dom.parseError.srcText);
      } 
      catch (e) { dom = null; }
   }
   else
      alert("oops");
   return dom;
}

window.onload = function() {
   var json;
   eval('a = {"e":null}');
   for (var i=0; i<xml.length; i++) {
      show(xml[i] + "\n\n" + 
           (json = xml2json(parseXml(xml[i]), "  ")) + "\n\n" +
           json2xml(eval('json='+json)));
   }
}

function show(s) { document.getElementById("out").innerHTML += (s+"\n").replace(/&/g, "&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\n/g, "<br/>") + "<hr/>"; }

</script>
</head>

<body>
<pre id="out"></pre>
</body>
</html>
